package com.lizongbo.seotijian.web.servlets.cgi;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lizongbo.seotijian.rules.whois.WhoisClient;
import com.lizongbo.seotijian.web.servlets.CGIServlet;

public class WhoisCgi extends AbstractCgi {

	@Override
	public void doCgiInternal(String cgiName, String param,
			HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String format = ".html";
		String domain = param;
		for (int i = 0; i < FORMATS.length; i++) {
			String paramL = param.toLowerCase();
			if (paramL.endsWith(FORMATS[i])) {
				format = FORMATS[i];
				domain = param.substring(0, param.length() - format.length());
			}
		}
		System.out.println("WhoisCgi doCgi for domain:" + domain);
		try {
			domain = java.net.URLDecoder.decode(domain, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		String whoisInfo = WhoisClient.getWhoisInfo(domain, true);
		req.setAttribute("canonicalUrl", CGIServlet.trimRepeatSlash(req
				.getRequestURL().toString()));
		req.setAttribute("domain", domain);
		req.setAttribute("whoisInfo", whoisInfo);
	}

	@Override
	public String getCgiName() {
		return "whois";
	}

}
